package demo.day1211;

import Common.DriverCommon;
import Util.CalculateLocation;
import org.neo4j.driver.*;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Relationship;

import java.util.List;

import static org.neo4j.driver.Values.parameters;

/**
 * 查询pitch pitch stadium 的demo
 */
public class SearchDemo3 {
    public static void main(String[] args) {
        String search1 = "pitch";
        String search2 = "pitch";
        String search3 = "building";
        String position1 = "东北";//下一个在前一个的方位
        String position2 = "东南";
        String position3 = "东";//模糊查找范围
        try (DriverCommon driverCommon = new DriverCommon()) {
            int all =0;
            Driver driver = driverCommon.getGraphDatabase();
            try (Session session = driver.session()) {
                try (Transaction tx = session.beginTransaction()) {
                    String cypherQuery = "MATCH (n {fclass: '"+search1+"'})-[r:NEAR]-(otherNode {fclass: '"+search2+"'}) return n ,otherNode,r";
                    Result result = tx.run(cypherQuery);
                    while (result.hasNext()) {
                        Record record = result.next();
                        Node node1 = record.get("n").asNode();

                        Node otherNode = record.get("otherNode").asNode();
                        List<Object> bbox =  node1.get("bbox").asList();//获取bbox

                        Relationship relationship = record.get("r").asRelationship();

                        String location =  relationship.get("location").asString();
                        if(node1 != null && otherNode != null ){
                            if(location.equals(position1)){
                                // 构建第二个查询，基于已找到的节点 node2
                                String secondQuery = "MATCH (otherNode)-[r:NEAR]-(stadiumNode {fclass: '"+search3+"'}) WHERE ID(otherNode) = $otherNodeId RETURN stadiumNode,r";
                                Result secondResult = tx.run(secondQuery,parameters("otherNodeId",otherNode.id() ));
                                while (secondResult.hasNext()) {
                                    Record secondRecord = secondResult.next();
                                    Node stadiumNode = secondRecord.get("stadiumNode").asNode();
                                    List<Object> otherBbox =  stadiumNode.get("bbox").asList();;//获取bbox
                                    Relationship relationship2 = secondRecord.get("r").asRelationship();
                                   String location2 = relationship2.get("location").asString();
                                    String location3 = CalculateLocation.GetDirection(bbox,otherBbox); //进一步筛选结果 构建第三个查询尾节点和头节点
                                   if (location2.equals(position2) && location3.contains(position3)){
                                       System.out.println(node1.get("name")+" "+otherNode.get("name")+" "+stadiumNode.get("name"));
                                        all++;
                                   }
                                }
                            }
                        }
                    }
                    System.out.println(all);
                    // 提交事务
                    tx.commit();
                }
            }
            driver.close();
        }
    }
}
